﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Election
{
	public partial class Form_Main : Form
	{
		public Form_Main()
		{
			InitializeComponent();
		}

		private void Form_Main_Load(object sender, EventArgs e)
		{
			Main_login form_login = new Main_login();
			form_login.ShowDialog();
			userInfo.Text = "User : " + orcl.user + "\nQuyền :\n";
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			comm.CommandText = "select GRANTED_ROLE from user_role_privs";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					userInfo.Text += "- " + reader["GRANTED_ROLE"].ToString() + "\n";
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private bool hadRole(string role)
		{
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			comm.CommandText = "select GRANTED_ROLE from user_role_privs";
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					if (reader["GRANTED_ROLE"].ToString() == role)
						return true;
				}
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		private void pictureBox1_Click(object sender, EventArgs e)
		{
			if (hadRole("NGUOIDIBAU"))
			{
				Election.NguoiDiBau.Form_NguoiDiBau_Main form = new NguoiDiBau.Form_NguoiDiBau_Main(orcl.user);
				form.ShowDialog();
			}
			else
				MessageBox.Show("Bạn không có quyền!");
		}

		private void pictureBox2_Click(object sender, EventArgs e)
		{
			if (hadRole("BANTOCHUC"))
			{
				Election.BanToChucBinhBau.Form_BanTCBB_Main form = new BanToChucBinhBau.Form_BanTCBB_Main();
				form.ShowDialog();
			}
			else
				MessageBox.Show("Bạn không có quyền!");
		}

		private void pictureBox3_Click(object sender, EventArgs e)
		{
			if (hadRole("TOLAPDANHSACH"))
			{
				ToLapDSNguoiDiBau.ToLapDanhSachNDB form = new ToLapDSNguoiDiBau.ToLapDanhSachNDB();
				form.ShowDialog();
			}
			else
				MessageBox.Show("Bạn không có quyền!");
		}

		private void pictureBox4_Click(object sender, EventArgs e)
		{
			if (hadRole("TOGIAMSAT"))
			{
				GiamSat form = new GiamSat();
				form.ShowDialog();
			}
			else
				MessageBox.Show("Bạn không có quyền!");
		}

		private void pictureBox5_Click(object sender, EventArgs e)
		{
			if (hadRole("TOTHEODOIKETQUA"))
			{
                ToTheoDoiKetQua.ToTheoDoiKetQua form = new ToTheoDoiKetQua.ToTheoDoiKetQua();
                form.ShowDialog();
			}
			else
				MessageBox.Show("Bạn không có quyền!");
		}

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Application.Restart();
        }
	}
}
